<?php
namespace Common\Model;
use Think\Model;
/**
 * 菜单Model
 */
class MenuModel extends Model
{
  private $_db = '';
  public function __construct(){
    $this->_db = M('menu');
  }
  public function addNewMenu($data = array()){
    $data['create_time'] = time();
    return $this->_db->add($data);
  }
  public function getAdminNav($map = ''){
    $map['status'] = 1;
    $map['cid'] = 1;
    $map['pid'] = 0;
    $nav = $this->_db->where($map)->order('menu_id')->select();
    foreach ($nav as $k => $v) {
      $pid = $this->getNavArr($v['menu_id'],1);
      if($pid){
        $nav[$k]['childNav'] = $pid;
      }
    }
    return $nav;
  }
  public function getMenuCount($map = array()){
      $map['status'] = array('neq',-1);
      return $this->_db->where($map)->count();
  }
  public function getMenuList($page,$pageSize,$data = array()){
    $map['status'] = array('neq',-1);
    if(isset($data['keyword'])){
      $map['title'] = array('like','%'.$data['keyword'].'%');
    }
    if(isset($data['cid'])){
      $map['cid'] = $data['cid'];
    }
    $offset = ($page-1)*$pageSize;
    return $this->_db->where($map)->order('menu_id')->limit($offset,$pageSize)->select();
  }
  public function getPid(){
    $map['pid'] = 0;
    $map['status'] =  array('neq','-1');
    return $this->_db->where($map)->order('menu_id desc')->select();
  }
/**
 * [getFMenuByPid description]
 * @param  [type] $pid [description]
 * @return [type]      [description]
 */
  public function getFMenuByPid($pid){
    if($pid != 0){
    $map['menu_id'] = $pid;
    $res = $this->_db->where($map)->find();
  }
    return $res?$res['title']:'一级菜单';
  }
  /**
   * [getMenuById description]
   * @param  [type] $id [description]
   * @return [type]     [description]
   */
  public function getMenuById($id){
    $map['menu_id']  = $id;
    $map['status'] = array('neq',-1);
    return $this->_db->where($map)->find();
  }
  /**
   * [updateMenu description]
   * @param  [type] $id   [description]
   * @param  [type] $data [description]
   * @return [type]       [description]
   */
  public function updateMenu($id,$data){
    if(!$id||!is_numeric($id)){
      throw_exception("ID不合法");
    }
    if(!$data||!is_array($data)){
      throw_exception("数据不合法");
    }
    $map['menu_id'] = $id;
    return $this->_db->where($map)->save($data);
  }
  /**
   * [getNavArr description]
   * @param  string  $pid    [description]
   * @param  integer $status [description]
   * @return [type]          [description]
   */
  private function getNavArr($pid = '',$status = 1){
      $map['pid'] = $pid;
      $map['status'] = $status;
      $map['cid'] = 1;
      $res = $this->_db->where($map)->order('menu_id desc')->select();
      return $res;
  }
  /**
   * 获取文章分类（前端可用菜单）
   * @param  array  $map 可选参数
   * @return [type]      [description]
   */
  public function getNewsCid($map = array()){
    $map['status'] = 1;
    $map['cid'] = 0;
    return $this->_db->where($map)->select();
  }
  public function getMenuName($map = array()){
    $map['status'] = 1;
    $menu = $this->_db->where($map)->find();
    return $menu['title'];
  }
}
